﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using IP20.Procdure;
using IP20.ViewModel;

namespace IP20.View
{
    /// <summary>
    /// HomeView.xaml 的交互逻辑
    /// </summary>
    public partial class HomeView : UserControl
    {
        public HomeView()
        {
            InitializeComponent();
            cbWriteFlash.Visibility = Visibility.Collapsed;
#if JQ8931314
            cbWriteFlash.Visibility = Visibility.Visible;
#endif
            var model = new HomeViewModel();
            this.DataContext = model;
            model.SNInputAction += SNInputAction;
            if (Global.UserChangeAction is null)
            {
                Global.UserChangeAction = new Action(() =>
                {
                    if (Global.User.UserLevel == UserLevel.Admin)
                    {
                        //cbOnlyViewTest.Visibility = Visibility.Visible;
                        //canUserSwitchChecked.Visibility = Visibility.Visible;
                        canUserStepTest.Visibility = Visibility.Visible;
                        //canUserSwitchChecked2.Visibility = Visibility.Visible;
                        canUserStepTest2.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        //cbOnlyViewTest.IsChecked = true;
                        //cbOnlyViewTest.Visibility = Visibility.Collapsed;
                        canUserStepTest.Visibility = Visibility.Collapsed;
                        //canUserSwitchChecked.Visibility = Visibility.Collapsed;
                        canUserStepTest2.Visibility = Visibility.Collapsed;
                        //canUserSwitchChecked2.Visibility = Visibility.Collapsed;
                    }
                });
            }
        }

        private void SNInputAction(int index)
        {
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                if (index == 1)
                {
                    this.tbSN.Text = "";
                    this.tbSN.Focus();
                }
                else
                {
                    this.tbSN2.Text = "";
                    this.tbSN2.Focus();
                }
            }));
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            InputLanguageManager.SetInputLanguage((DependencyObject)sender, CultureInfo.GetCultureInfo("en-US"));
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dataGrid.SelectedItem != null)
            {
                dataGrid.ScrollIntoView(dataGrid.SelectedItem);
            }
        }

        private void DataGrid_SelectionChanged2(object sender, SelectionChangedEventArgs e)
        {
            if (dataGrid2.SelectedItem != null)
            {
                dataGrid2.ScrollIntoView(dataGrid2.SelectedItem);
            }
        }

        private void cbDebug_Checked(object sender, RoutedEventArgs e)
        {
            (sender as CheckBox).Content = "烧录";
        }

        private void cbDebug_Unchecked(object sender, RoutedEventArgs e)
        {
            (sender as CheckBox).Content = "测试";
        }
    }
}